﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using BrainTechLLC.Silverlight.Controls;

namespace BrainTechLLC.Silverlight.ClientApplication
{
    public partial class ShowStatesControl : BaseDataVisualizer, ICollectionDataVisualizer
    {
        public override void SetVisualizedData(DataVisualizer vis, object o) { States = o as ObservableCollection<State>; base.SetVisualizedData(vis, o); }
        public Type VisualizeType { get { return typeof(State); } }
        public string VisualizerName { get { return "ShowStatesControl"; } }

        public static readonly DependencyProperty StatesProperty = DependencyProperty.Register(
            "States", typeof(ObservableCollection<State>), typeof(ShowStatesControl), new PropertyMetadata(null, new PropertyChangedCallback(StatesChanged)));

        public static void HandleChange<T2>(DependencyObject d, DependencyPropertyChangedEventArgs e, Action<ShowStatesControl, T2> action)
        {
            d.HandleChange<ShowStatesControl, T2>(e, action, (t) => t.UpdateBindings());
        }

        public ObservableCollection<State> States
        {
            get { return (ObservableCollection<State>)GetValue(StatesProperty); }
            set { SetValue(StatesProperty, value); }
        }

        private static void StatesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HandleChange<ObservableCollection<State>>(d, e, (t, val) => t.States = val);
        }

        public override void UpdateBindings()
        {
            if (lstStates != null)
                lstStates.ItemsSource = States;
        }

        public void BindToStateData(StateCollection states)
        {
            ObservableCollection<State> results = new ObservableCollection<State>();

            var v = states.AllGameThingsInCollection;
            foreach (var item in v)
            {
                State state = item as State;

                if (state != null)
                    results.Add(state);
            }

            States = results;
        }

        public override string TitleText
        {
            get
            {
                return string.Format("{0} States ({1})", States.Count, States.BuildList((a) => a.StateName).MaxLength(30, true));
            }
        }

        public ShowStatesControl()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(ShowStatesControl_Loaded);
        }

        void ShowStatesControl_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void lstStates_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (lstStates.SelectedItem == null)
                return;

            State state = (State)lstStates.SelectedItem;
        }

        private void lstStates_Loaded(object sender, RoutedEventArgs e)
        {
            FrameworkElement t = sender as FrameworkElement;
            t.StretchListBox();
        }

        private void Border_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}